<template>
	<div>
		<el-popover v-model="visible" title="最近停机记录" width="800px" @show="onShow" @hide="onHide">
			<jk-table :row-config="{isCurrent: true}" :data="stopRecordList" :height="300" @current-change="onSelectChange">
				<vxe-column :width="100" field="machineName" title="设备"></vxe-column>
				<vxe-column :min-width="200" field="productName" title="设备品种"></vxe-column>
				<vxe-column :width="160" field="systemStartDateTime" title="系统判断开台时间"></vxe-column>
				<vxe-column :width="160" field="systemEndDateTime" title="系统判断了机时间"></vxe-column>
				<vxe-column :min-width="160" field="batchCode" title="设备批号"></vxe-column>
				<vxe-column align="center" title="操作" width="100">
					<template slot-scope="scope">
						<el-button type="default" size="mini" @click="onSelectChange({ row: scope.row })">选择</el-button>
					</template>
				</vxe-column>
			</jk-table>
			<div style="text-align: right; margin: 10px 0 0 0;">
				<el-button size="mini" type="text" @click="visible = false">取消</el-button>
				<el-button type="primary" size="mini" @click="onConfirmBtn">确定</el-button>
			</div>
			<el-button slot="reference" icon="el-icon-more" type="default"></el-button>
		</el-popover>
	</div>
</template>
<script>
    import { tipsBar } from '../../../../../utils/jk/jk-tool';
    import { productionMachineShiftProductOutputGetWaitOpenList } from '../../../../../api/production/order';
    import splitProductName from '../../../supplementaryPlanList/splitProductName';

    export default {
        mixins: [splitProductName],
        props: {
            formData: {
                type: Object,
                default() {
                    return {};
                }
            }
        },
        data() {
            return {
                visible: false,
                selectItem: {},
                stopRecordList: []
            };
        },
        methods: {
            onHide() {
                this.selectItem = {};
                this.stopRecordList.forEach(x => {
                    this.$set(x, 'isCheck', false);
                });
            },
            onShow() {
                this.getOpenTimeRequest();
            },
            onConfirmBtn() {
                if (Object.keys(this.selectItem).length) {
                    this.$emit('on-confirm', JSON.parse(JSON.stringify(this.selectItem)));
                    this.visible = false;
                } else {
                    tipsBar(this, 'custom', 'warning', '请选择!');
                }
            },
            getOpenTimeRequest() {
                const productName = this.getSplitProductName(this.formData.productName);
                return productionMachineShiftProductOutputGetWaitOpenList({
                    machineNameOrCode: this.formData.machineName,
                    // productName: productName,
                    pageNo: 1,
                    pageSize: 50
                }).then(res => {
                    if (res.data.status === 200) {
                        this.stopRecordList = res.data.res || [];
                    }
                });
            },
            onSelectChange({ row }) {
                this.selectItem = row;
                this.stopRecordList.forEach(x => {
                    this.$set(x, 'isCheck', false);
                    if (x.id === row.id) {
                        this.$set(x, 'isCheck', true);
                    }
                });
            }
        }
    };
</script>
